<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <canvas id="web" style="background-color: blue;"></canvas>
    <script id="vertexShader" type="x-shader/x-vertex">
        attribute vec4 apos;
        void main(){
            gl_Position= vec4(apos.x, apos.y -0.5, apos.z, 1);
        }
    </script>
    <script id="fragmentShader" type="x-shader/x-frament">
        void main(){
            gl_FragColor = vec4(1.0,1.0,0.0,0.7);
        }
    </script>
        
    <script>
        const initShader = (gl, vertexShadeSource, fragmentShadeSource) =>{
            let vertexShader = gl.createShader(gl.VERTEX_SHADER);
            let framentShader = gl.createShader(gl.FRAGMENT_SHADER);
            gl.shaderSource(vertexShader, vertexShadeSource);
            gl.shaderSource(framentShader, fragmentShadeSource);
            gl.compileShader(vertexShader);
            gl.compileShader(framentShader);

            let program = gl.createProgram();
            gl.attachShader(program, vertexShader);
            gl.attachShader(program, framentShader);
            gl.linkProgram(program);
            gl.useProgram(program);
            return program;
        }

        // 获取 gl 上下文对象
        let canvas = document.getElementById('web');
        let gl = canvas.getContext('webgl');
        // 获取 着色器 代码
        let vertexShadeSource = document.getElementById("vertexShader").innerText;
        let fragmentShadeSource = document.getElementById('fragmentShader').innerText;
        // 初始化进程
        let program = initShader(gl, vertexShadeSource, fragmentShadeSource);

        let points = new Float32Array([
                0.1, 0.1, 0.3,
                0.9, 0.1, 0.5,
                0.9, 0.9, 0.0
            ]);
        let posLocation = gl.getAttribLocation(program, 'apos');

        let buffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
        gl.bufferData(gl.ARRAY_BUFFER, points, gl.STATIC_DRAW);
        gl.vertexAttribPointer(posLocation, 3, gl.FLOAT, false, 0, 0)
        gl.enableVertexAttribArray(posLocation);
        
        // 开启绘制
        gl.drawArrays(gl.TRIANGLES, 0, 3);
        
    </script>
</body>
</html>